package com.wx.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wx.model.Books;
import com.wx.model.Order;
import com.wx.model.Users;
import com.wx.serviceImpl.AddOrderServiceImpl;

/**
 * Servlet implementation class AddOrderServlet
 */
@WebServlet("/servlet/AddOrderServlet")
public class AddOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddOrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String adress = request.getParameter("adress");
		String home = request.getParameter("home");
		int postcode = Integer.parseInt(request.getParameter("postcode"));
		HttpSession session = request.getSession();
		List<Books> books = (List<Books>)session.getAttribute("books");
		int cartcount = Integer.parseInt((String)session.getAttribute("cartcount"));
		Users user = (Users)session.getAttribute("login");
		for(Books book : books){
			Order order = new Order();
			order.setBookid(book.getBookid());
			order.setUserid(user.getUserid());
			order.setName(name);
			order.setPhone(phone);
			order.setHome(home);
			order.setBookcount(cartcount);
			try {
				new AddOrderServiceImpl().addOrder(order);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletreResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
